//
//  WX_UserTableView.m
//  ZITHER
//
//  Created by 吴凡 on 2019/2/14.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "WX_UserTableView.h"
#import "Ugc_Friends_OnePicCell_WX.h"
#import "Ugc_Friends_FourPicCell_WX.h"
#import "Ugc_Friends_MorePicCell_WX.h"

#import "MainFirst_MagazineList_PGCCell_WX.h"
#import "MainFirst_MagazineList_UgcCell_WX.h"

#import "MainFirst_Magazine_VC_WX.h"
#import "Ugc_Detail_VC_WX.h"
#import "UgcMainModel.h"
@implementation WX_UserTableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self defaultConfiguration];
    }
    return self;
}

-(void)setIndexType:(NSString *)indexType{
    _indexType = indexType;
    [self.mj_header beginRefreshing];
}

-(void)loadData:(BOOL)type{
    @weakify(self);
    [[serviceDefualt loadRecommendcreationlistPage:0 reponseObjctClass:[CoverResultModelSub class]]subscribeNext:^(ResponseData * x) {
        @strongify(self);

        if (x.success) {
            
            [self setNoMoreDataBg:YES NoDataState:NoMoreDataStateDefault];
            if (self.model == nil) {
                self.model = [[CoverCreateListModel alloc]init];
            }
            CoverResultModelSub * model = [[CoverResultModelSub alloc]init];
            model = [CoverResultModelSub yy_modelWithDictionary:x.responseRawData];
            if (model.data.count == 0) {
                
                if (self.page == 0 ) {
                    [self setNoMoreDataBg:NO NoDataState:NoMoreDataStateDefault];
                    [Tools showTips:@"暂无更多数据"];
                    [self endRefreshing];
                    [self.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self.mj_footer endRefreshingWithNoMoreData];
                }
                return ;
            }else{
                if (self.page == 0) {
                    self.model.result = model;
                }else{
                    for (CoverCreateListModelSub *modelSub in model.data) {
                        [self.model.result.list addObject:modelSub];
                    }
                }
            }
            
            //                [self setHeadView];
            
            [self endRefreshing];
            [self reloadData];
        }else{
            [Tools showTips:x.errorInfo];
            [self endRefreshing];
        }
        
    }];
}


-(void)setHeadV{
    
}

-(void)endRefreshing{
    [self.mj_header endRefreshing];
    [self.mj_footer endRefreshing];
}

-(void)defaultConfiguration{
    
    self.delegate = self;
    self.dataSource = self;
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.rowHeight = UITableViewAutomaticDimension;
    self.estimatedRowHeight = 1000;
    [self registerNib:[UINib nibWithNibName:@"UgcFriendsTbvCell" bundle:nil] forCellReuseIdentifier:@"UgcFriendsTbvCell"];
    [self registerNib:[UINib nibWithNibName:@"Ugc_Friends_OnePicCell_WX" bundle:nil] forCellReuseIdentifier:@"Ugc_Friends_OnePicCell_WX"];
    [self registerNib:[UINib nibWithNibName:@"Ugc_Friends_FourPicCell_WX" bundle:nil] forCellReuseIdentifier:@"Ugc_Friends_FourPicCell_WX"];
    [self registerNib:[UINib nibWithNibName:@"Ugc_Friends_MorePicCell_WX" bundle:nil] forCellReuseIdentifier:@"Ugc_Friends_MorePicCell_WX"];
    [self registerNib:[UINib nibWithNibName:@"MainFirst_MagazineList_PGCCell_WX" bundle:nil] forCellReuseIdentifier:@"MainFirst_MagazineList_PGCCell_WX"];
    [self registerNib:[UINib nibWithNibName:@"MainFirst_MagazineList_UgcCell_WX" bundle:nil] forCellReuseIdentifier:@"MainFirst_MagazineList_UgcCell_WX"];
    
    self.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 20)];
    
    @weakify(self);
    self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        self.page = 0;
        [self loadData:YES];
    }];
    
    self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        @strongify(self);
        self.page ++;
        [self loadData:NO];
    }];
    
}

-(void)setHeadView{
    
    if ([self.indexType isEqualToString:@"0"]) {
        if ( [self.model.recommend isEqualToString:@"1"]) {
            if (kUSER_INFO_MANAGER.userIsLogin) {
                [self.headView isType:0];
            }else{
                [self.headView isType:1];
            }
            
        }else{
            [self.headView isType:2];
        }
        self.tableHeaderView = self.headView;
        [self reloadData];
        return;
    }
    self.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 1)];
    
}

#pragma MARK - 数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
//    if (![self.indexType isEqualToString:@"2"]) {
        return self.model.result.data.count;
//    }
//    return self.arrM.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CoverCreateListModelSub *model = self.model.result.data[indexPath.row];
    if ([model.relType isEqualToString:@"1"]) {
        return [self getMainFirst_MagazineList_UgcCell_WX:indexPath];
    }
    return [self getMainFirst_MagazineList_PGCCell_WX:indexPath];
}


-(MainFirst_MagazineList_UgcCell_WX*)getMainFirst_MagazineList_UgcCell_WX:(NSIndexPath*)indexPath{
    CoverCreateListModelSub *modelSub = self.model.result.data[indexPath.row];
    
    static NSString *sectionCell = @"MainFirst_MagazineList_UgcCell_WX";
    MainFirst_MagazineList_UgcCell_WX *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[MainFirst_MagazineList_UgcCell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    cell.recommend = @"1";
    cell.model = modelSub;
    return cell;
}




-(MainFirst_MagazineList_PGCCell_WX*)getMainFirst_MagazineList_PGCCell_WX:(NSIndexPath*)indexPath{
    
    CoverCreateListModelSub *modelSub = self.model.result.data[indexPath.row];
    
    static NSString *sectionCell = @"MainFirst_MagazineList_PGCCell_WX";
    MainFirst_MagazineList_PGCCell_WX *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[MainFirst_MagazineList_PGCCell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    cell.recommend = @"1";

//    cell.recommend = self.model.recommend;

//    if ([self.indexType isEqualToString:@"0"])
//    {
//        if (kUSER_INFO_MANAGER.userIsLogin) {
//            
//            if ([self.model.recommend isEqualToString:@"0"]) {
//                cell.recommend = @"2";
//            }else{
//                cell.recommend = @"1";
//            }
//            
//        }else{
//            cell.recommend = @"2";
//        }
//    }else{
//        cell.recommend = self.model.recommend;
//    }
    cell.model = modelSub;
    return cell;
}


-(Ugc_Friends_OnePicCell_WX*)getFriendsOneCell:(NSIndexPath*)indexPath{
    
    static NSString *OnePicCell = @"Ugc_Friends_OnePicCell_WX";
    Ugc_Friends_OnePicCell_WX *cell = [self dequeueReusableCellWithIdentifier:OnePicCell];
    if (cell == nil) {
        cell = [[Ugc_Friends_OnePicCell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:OnePicCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    if ([self.indexType isEqualToString:@"2"]) {
        UgcMainListModel *model = self.arrM[indexPath.row];
        cell.model = model;
    }else{
        CoverCreateListModelSub *modelSub = self.model.result.list[indexPath.row];
        cell.modelSub = modelSub;
    }
    
    return cell;
}

-(Ugc_Friends_FourPicCell_WX*)getFriendsFourPicCell:(NSIndexPath*)indexPath{
    
    static NSString *fourPicCell = @"Ugc_Friends_FourPicCell_WX";
    Ugc_Friends_FourPicCell_WX *cell = [self dequeueReusableCellWithIdentifier:fourPicCell];
    if (cell == nil) {
        cell = [[Ugc_Friends_FourPicCell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:fourPicCell];
    }
    
    @weakify(cell);
    
    @weakify(self);
    
    [cell setShowBigPic:^(NSIndexPath * _Nonnull indexPath, NSUInteger index) {
        @strongify(cell);
        @strongify(self);
        self.type = NO;
        [self.arrImageView removeAllObjects];
        [self.arrImageUrl removeAllObjects];
        
        self.arrZipImageView = cell.arrImageView;
        
        for (int i = 0; i<cell.model.fileList.count; i++) {
            NSString *str = [NSString stringWithFormat:@"%@%@",RootUrlImage,cell.model.fileList[i]];
            UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
            [imageV sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
            [self.arrImageView addObject:imageV];
            [self.arrImageUrl addObject:str];
        }
        
        [self showBigImage:index];
    }];
    
    cell.indexPath = indexPath;
    
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    if ([self.indexType isEqualToString:@"2"]) {
        UgcMainListModel *model = self.arrM[indexPath.row];
        cell.model = model;
    }else{
        CoverCreateListModelSub *modelSub = self.model.result.list[indexPath.row];
        cell.modelSub = modelSub;
    }
    return cell;
}

-(Ugc_Friends_MorePicCell_WX*)getFriendsMorePicCell:(NSIndexPath*)indexPath{
    static NSString *sectionCell = @"Ugc_Friends_MorePicCell_WX";
    //        NSLog(@" ========== %@",sectionCell);
    Ugc_Friends_MorePicCell_WX *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[Ugc_Friends_MorePicCell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.indexPath = indexPath;
    @weakify(cell);
    @weakify(self);
    [cell setShowBigPic:^(NSIndexPath * _Nonnull indexPath, NSUInteger index) {
        @strongify(cell);
        @strongify(self);
        self.type = NO;
        [self.arrZipImageView removeAllObjects];
        [self.arrImageView removeAllObjects];
        [self.arrImageUrl removeAllObjects];
        
        for (int i = 0; i<cell.model.fileList.count; i++) {
            NSString *str = [NSString stringWithFormat:@"%@%@",RootUrlImage,cell.model.fileList[i]];
            UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
            [imageV sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
            [self.arrImageView addObject:imageV];
            [self.arrImageUrl addObject:str];
            [self.arrZipImageView addObject:cell.arrImageView[i]];
        }
        
        [self showBigImage:index];
    }];
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    if ([self.indexType isEqualToString:@"2"]) {
        UgcMainListModel *model = self.arrM[indexPath.row];
        cell.model = model;
    }else{
        CoverCreateListModelSub *modelSub = self.model.result.list[indexPath.row];
        cell.modelSub = modelSub;
        
    }    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CoverCreateListModelSub *modelSub = self.model.result.data[indexPath.row];
    if ([modelSub.relType isEqualToString:@"2"]) {
        MainFirst_Magazine_VC_WX *vc = [[MainFirst_Magazine_VC_WX alloc]init];
        vc.pgcId = modelSub.relId;
        [self.getNav pushViewController:vc animated:YES];
    }else{
        Ugc_Detail_VC_WX *vc = [[Ugc_Detail_VC_WX alloc]init];
        vc.ugcId = modelSub.relId;
        vc.userID = modelSub.userId;
        [self.getNav pushViewController:vc animated:YES];
    }
}

-(QMUIImagePreviewViewController *)imagePreviewViewController{
    if (!_imagePreviewViewController) {
        _imagePreviewViewController = [[QMUIImagePreviewViewController alloc] init];
        _imagePreviewViewController.imagePreviewView.delegate = self;
    }
    return _imagePreviewViewController;
}

-(void)showBigImage:(NSInteger)selectIndex{
    UIImageView *imageV = self.arrZipImageView[selectIndex];
    @weakify(self);
    self.imagePreviewViewController.customGestureExitBlock = ^(QMUIImagePreviewViewController *aImagePreviewViewController, QMUIZoomImageView *currentZoomImageView) {
        @strongify(self);
        NSInteger index = aImagePreviewViewController.imagePreviewView.currentImageIndex;
        UIImageView *imageV1 = self.arrZipImageView[index];
        
        UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
        CGRect rect=[imageV1 convertRect: imageV1.bounds toView:window];
        
        [aImagePreviewViewController exitPreviewToRectInScreenCoordinate:rect];
        self.imagePreviewViewController = nil;
    };
    self.imagePreviewViewController.imagePreviewView.currentImageIndex =  selectIndex;// 默认查看的图片的 index
    
    UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
    
    CGRect rect=[imageV convertRect: imageV.bounds toView:window];
    
    [self.imagePreviewViewController startPreviewFromRectInScreenCoordinate:rect cornerRadius:imageV.layer.cornerRadius];
}


#pragma mark - <QMUIImagePreviewViewDelegate>

- (NSUInteger)numberOfImagesInImagePreviewView:(QMUIImagePreviewView *)imagePreviewView {
    return self.arrImageView.count;
}

- (void)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView renderZoomImageView:(QMUIZoomImageView *)zoomImageView atIndex:(NSUInteger)index {
    // 模拟异步加载的情况
    zoomImageView.reusedIdentifier = @(index);
    [zoomImageView showLoading];
    if ([zoomImageView.reusedIdentifier isEqual:@(index)]) {
        //        UIImageView *imageV = ;
        [self.arrImageView[index] sd_setImageWithURL:[NSURL URLWithString:self.arrImageUrl[index]] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            [zoomImageView hideEmptyView];
            zoomImageView.image = self.arrImageView[index].image;
            
        }];
    }
    
}

- (QMUIImagePreviewMediaType)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView assetTypeAtIndex:(NSUInteger)index {
    return QMUIImagePreviewMediaTypeImage;
}

#pragma mark - <QMUIZoomImageViewDelegate>

- (void)singleTouchInZoomingImageView:(QMUIZoomImageView *)zoomImageView location:(CGPoint)location {
    //点击缩回
    self.imagePreviewViewController.customGestureExitBlock(self.imagePreviewViewController, zoomImageView);
    [self.arrImageView removeAllObjects];
    [self.arrImageUrl removeAllObjects];
    [[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];
    
}



- (NSMutableArray<UIImageView *> *)arrImageView{
    if (!_arrImageView) {
        _arrImageView = [[NSMutableArray alloc]init];
    }
    return _arrImageView;
}

-(NSMutableArray *)arrZipImageView{
    if (!_arrZipImageView) {
        _arrZipImageView = [[NSMutableArray alloc]init];
    }
    return _arrZipImageView;
}

-(NSMutableArray *)arrImageUrl{
    if (!_arrImageUrl) {
        _arrImageUrl = [[NSMutableArray alloc]init];
        
    }
    return _arrImageUrl;
}

-(Ugc_Friends_TbvHead_WX *)headView{
    if (!_headView) {
        _headView = [[Ugc_Friends_TbvHead_WX alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 200)];
    }
    return _headView;
}

@end
